<?php
/**
 * 
 * IndexAction.class.php (前台首页)
 *
 * @package      	jgsat
 * @author          liuxun QQ:147613338 <admin@jgsat.cn>
 * @copyright     	Copyright (c) 2008-2011  (http://www.jgsat.com)
 * @license         http://www.jgsat.com/license.txt
 * @version        	jgsat企业网站管理系统 v2.1 2011-03-01 jgsat.cn $
 */
if(!defined("JGsat")) exit("Access Denied");
class CaseAction extends BaseAction
{

  
    public function index($catid='',$module='')
    {
		$url_s='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$this->assign('url_s',$url_s);
		$this->Urlrule =F('Urlrule');
		if(empty($catid)) $catid =  intval($_REQUEST['id']);

		$p= max(intval($_REQUEST[C('VAR_PAGE')]),1);
		if($catid){
			$cat = $this->categorys[$catid];

			$bcid = explode(",",$cat['arrparentid']); 
                        $jgbcid = $bcid; 
			$bcid = $bcid[1]; 
			if($bcid == '') $bcid=intval($catid);
			if(empty($module))$module=$cat['module'];
			$this->assign('module_name',$module);
			unset($cat['id']);
			$this->assign($cat);
			$cat['id']=$catid;
			$this->assign('jgbcid',$jgbcid);
			$this->assign('catid',$catid);
			$this->assign('bcid',$bcid);
		}
		if($cat['readgroup'] && $this->_groupid!=1 && !in_array($this->_groupid,explode(',',$cat['readgroup']))){$this->assign('jumpUrl',URL('User-Login/index'));$this->error (L('NO_READ'));}
		$fields = F($this->mod[$module].'_Field');
		foreach($fields as $key=>$r){
			$fields[$key]['setup'] =string2array($fields[$key]['setup']);
		}
		$this->assign ( 'fields', $fields); 


		$seo_title = $cat['title'] ? $cat['title'] : $cat['catname'];
		$this->assign ('seo_title',$seo_title);
		$this->assign ('seo_keywords',$cat['keywords']);
		$this->assign ('seo_description',$cat['description']);
				
 
			if($catid){
				$seo_title = $cat['title'] ? $cat['title'] : $cat['catname'];
				$this->assign ('seo_title',$seo_title);
				$this->assign ('seo_keywords',$cat['keywords']);
				$this->assign ('seo_description',$cat['description']);
				

				$where = " status=1 and catid=$catid ";


				$itemType=$_GET['itemType'];
				$itemArea=$_GET['itemArea'];
				$budget=$_GET['budget'];
				$style=$_GET['style'];
				if($style){
					$where .= " and style=".$style;
				}
				if($itemType){
					$where .= "  and itemType=".$itemType;
				}

				if($itemArea){
					$list = M('Label')->where('id='.$itemArea)->find();
                    $result = preg_replace('/([\x80-\xff]*)/i','',$list['title']);
					$area=explode("-",$result);
					$min=$area[0];
					$max= !empty($area[1])? $area[1]:'10000000000000';
					if(empty($area[1]) && $min<150){
						$max=$min;
						$min=0;

					}

                    $where .= "  and itemArea>=".$min." and itemArea<=".$max;
				}
				if($budget){
					$list_bud = M('Label')->where('id='.$budget)->find();
					$bud = preg_replace('/([\x80-\xff]*)/i','',$list_bud['title']);
					$bud=explode("-",$bud);
					//print_r($bud);exit;
					$min_bud=$bud[0];
					$max_bud=$bud[1];
					$where .= "  and budget>=".$min_bud." and budget<=".$max_bud;
				}

				
             // echo $where;exit;
				if(empty($cat['listtype'])){
					$this->dao= M($module);
					$count = $this->dao->where($where)->count();
					//echo $this->dao->getLastSql();exit;

					if($count){
						//import ( "@.ORG.Page" );
						$listRows =  !empty($cat['pagesize']) ? $cat['pagesize'] : C('PAGE_LISTROWS');
						$of=$listRows*($p-1);
						//$page = new Page ( $count, $listRows );
						//$pages = $page->preshow();
						$field = '*';
						$order='listorder ,createtime desc,id desc';
						if($_GET['order']){
							$order=$_GET['order'].' desc';
						}

						$list = $this->dao->field($field)->where($where)->order($order)->limit($of , $listRows)->select();
						//echo $this->dao->getLastSql();exit;
						//$this->assign('pages',$pages);
						$this->assign('list',$list);
					}
					$template_r = 'list';
				}else{
					$template_r = 'index';
				}
			}else{
				$template_r = 'list';
			}
			$template = $cat['template_list'] ? $cat['template_list'] : $template_r;

             //城市
		    $bcity_hot=M("Area")->field("*")->where("hot=1 ")->order("id desc")->limit("50")->select();
		    $this->assign('bcity_hot', $bcity_hot);

             if($p>1){
				 foreach($list as &$v){
					 $user = M('User')->where('id='.$v['userid'])->find();
					 //name="Label" where="genre=17" id="v" limit="50"itemType
					 $itemType = M('Label')->where('id='.$v['itemType'])->find();
					 $style = M('Label')->where('id='.$v['style'])->find();
					 $v['itemType']=$itemType['title'];
					 $v['style']=$style['title'];
					 $v['avatar']=$user['avatar'];
					  if($catid==1){
					 $v['date']=date("m月d日 H:i:s",$v['createtime']);
					  }else{
						  $v['date']=date("m月d日",$v['createtime']);
					  }
                 }
				  $url_s=unsetParam('p', $url_s);
				   $n=$p+1;
				  $url=$url_s.'&p='.$n;
                  $data = array('list' => $list, 'p' => $url);
				// print_r($data);exit;
				 echo json_encode($data );//转换格式
			 }else{
			$this->display($module.':'.$template.$catid);
			 }
		}


	public function show($id='',$module='')
	{
		//print_r($_COOKIE);exit;
		$this->Urlrule =F('Urlrule');
		$p= max(intval($_REQUEST[C('VAR_PAGE')]),1);
		$id = $id ? $id : intval($_REQUEST['id']);
		$module = $module ? $module : MODULE_NAME;
		$this->assign('module_name',$module);
		$this->dao= M($module);;
		$data = $this->dao->find($id);
        
		 if(!$_SESSION ['authId']){
			 if($data['status']!=1){
				 $this->error(L('作品不存在'));
				 }
		 }
		$catid = $data['catid'];
		$cat = $this->categorys[$data['catid']];
		if(empty($cat['ishtml']))$this->dao->where("id=".$id)->setInc('hits'); //添加点击次数
		$bcid = explode(",",$cat['arrparentid']);
		$jgbcid = $bcid;
		$bcid = $bcid[1];
		if($bcid == '') $bcid=intval($catid);

		if($data['readgroup']){
			if($this->_groupid!=1 && !in_array($this->_groupid,explode(',',$data['readgroup'])) )$noread=1;
		}elseif($cat['readgroup']){
			if($this->_groupid!=1 && !in_array($this->_groupid,explode(',',$cat['readgroup'])) )$noread=1;
		}
		if($noread==1){$this->assign('jumpUrl',URL('User-Login/index'));$this->error (L('NO_READ'));}

		$chargepoint = $data['readpoint'] ? $data['readpoint'] : $cat['chargepoint'];
		if($chargepoint && $data['userid'] !=$this->_userid){
			$user = M('User');
			$userdata =$user->find($this->_userid);
			if($cat['paytype']==1 && $userdata['point']>=$chargepoint){
				$chargepointok = $user->where("id=".$this->_userid)->setDec('point',$chargepoint);
			}elseif($cat['paytype']==2 && $userdata['amount']>=$chargepoint){
				$chargepointok = $user->where("id=".$this->_userid)->setDec('amount',$chargepoint);
			}else{
				$this->error (L('NO_READ'));
			}
		}

		$seo_title = $data['title'].'-'.$cat['catname'];
		//echo $seo_title;exit;
		$this->assign ('seo_title',$seo_title);
		$this->assign ('seo_keywords',$data['keywords']);
		$this->assign ('seo_description',$data['description']);
		$this->assign ( 'fields', F($cat['moduleid'].'_Field') );


		$fields = F($this->mod[$module].'_Field');
		//print_r($data);exit;
		foreach($data as $key=>$c_d){
			$setup='';
			$fields[$key]['setup'] =$setup=string2array($fields[$key]['setup']);

			if($setup['fieldtype']=='varchar' && $fields[$key]['type']!='text'){
				$data[$key.'_old_val'] =$data[$key];
				$data[$key]=fieldoption($fields[$key],$data[$key]);
			}elseif($fields[$key]['type']=='images' || $fields[$key]['type']=='files'){
				if(!empty($data[$key])){
					$p_data=explode(':::',$data[$key]);
					$data[$key]=array();
					foreach($p_data as $k=>$res){
						$p_data_arr=explode('|',$res);
						$data[$key][$k]['filepath'] = $p_data_arr[0];
						$data[$key][$k]['filename'] = $p_data_arr[1];
					}
					unset($p_data);
					unset($p_data_arr);
				}
			}
			unset($setup);
		}
		$this->assign('fields',$fields);


		//手动分页
		$CONTENT_POS = strpos($data['content'], '[page]');
		if($CONTENT_POS !== false) {

			$urlrule = geturl($cat,$data,$this->Urlrule);
			$urlrule =  str_replace('%7B%24page%7D','{$page}',$urlrule);
			$contents = array_filter(explode('[page]',$data['content']));
			$pagenumber = count($contents);
			for($i=1; $i<=$pagenumber; $i++) {
				$pageurls[$i] = str_replace('{$page}',$i,$urlrule);
			}
			$pages = content_pages($pagenumber,$p, $pageurls);
			//判断[page]出现的位置是否在文章开始
			if($CONTENT_POS<7) {
				$data['content'] = $contents[$p];
			} else {
				$data['content'] = $contents[$p-1];
			}
			$this->assign ('pages',$pages);
		}
		if(MODULE_NAME=="Topic"){
			//评论
			$jgcommentcount=M("Comment")->where("contype='".MODULE_NAME."' and conid=".$id)->count();
			$this->assign('jgcommentcount',$jgcommentcount);
		}
		if(!empty($data['template'])){
			$template = $data['template'];
		}elseif(!empty($cat['template_show'])){
			$template = $cat['template_show'];
		}else{
			$template =  'show';
		}

		$this->assign('jgbcid',$jgbcid);
		$this->assign('catid',$catid);
		$this->assign ($cat);
		$this->assign('bcid',$bcid);

		$this->assign ($data);
//print_r($data);exit;
		$this->display($module.':'.$template.$catid);
	}


   
 
 
}
?>